﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Web
{
    public partial class admin_indexslide : System.Web.UI.Page
    {
        public string TopTips = "";//首页图片信息管理
        public string SecondTips = "图片管理";//图片管理

        protected void Page_Load(object sender, EventArgs e)
        {
            string action = "index";
            if (Request.QueryString["action"] == null)
            {
                action = "index";
            }
            else {
                action = Request.QueryString["action"].ToString();
            }
            

            if (!IsPostBack)
            {
                BindNotice();
            }

            BindData(action);
        }

        private void BindData(string action)
        {
            if (action == "index")
            {
                BindIndexPic();

                TopTips = "首页图片信息管理";
                SecondTips = "图片管理";
            }
            else if(action == "about"){ 

            }
        }
        
        private void BindIndexPic()
        {
            IEnumerable<IndexPicInfo> pics = IndexPicInfoLogic.GetAllIndexPicInfo();
            IndexPicRpt.DataSource = pics;
            IndexPicRpt.DataBind();
        }

        private void BindNotice()
        {
            noticefail.Visible = false;
            noticesuccess.Visible = false;
        }

        

        protected void btnSubmit_OnClick(object sender, EventArgs e)
        {
            string action = "index";
            if (Request.QueryString["action"] == null)
            {
                action = "index";
            }
            else
            {
                action = Request.QueryString["action"].ToString();
            }
            if (action == "index")
            {
                IndexPicManager();
            }
        }

        public void IndexPicManager()
        {
            int flag1 = 1, flag2 = 1;
            //if (Request.Form["picadd[]"] != null)
            //{
            //    flag1 = 0;
            //    string[] picadd = Request.Form["picadd[]"].Split(',');
            //    int count = picadd.Count();
            //    int addsuccess = 0;
            //    for (int i = 0; i < count; i++)
            //    {
            //        IndexPicInfo pic = new IndexPicInfo();
            //        pic.PicURL = picadd[i];
                    
            //        addsuccess += IndexPicInfoLogic.InsertIndexPicInfo(pic);
            //    }
            //    if (addsuccess == count)
            //    {
            //        flag1 = 1;
            //    }
            //}
            if (Request.Form["picurl[]"] != null)
            {
                flag1 = 0;
                string[] picurl = Request.Form["picurl[]"].Split(',');
                string[] pictitle = Request.Form["pictitle[]"].Split(',');
                string[] picorder = Request.Form["picorder[]"].Split(',');
                string[] piclink = Request.Form["piclink[]"].Split(',');
                int count = picurl.Count();
                int addsuccess = 0;
                for (int i = 0; i < count; i++)
                {
                    IndexPicInfo pic = new IndexPicInfo();
                    pic.PicURL = picurl[i];
                    pic.PicLink = piclink[i];
                    pic.PicOrder = Convert.ToInt32(picorder[i]);
                    pic.PicTitle = pictitle[i];
                    IndexPicInfo info = IndexPicInfoLogic.GetIndexPicInfoFromUrl(pic.PicURL);
                    if (info != null && info.PicURL == pic.PicURL)
                    {
                        pic.IndexPicID = info.IndexPicID;
                        addsuccess += IndexPicInfoLogic.UpdateIndexPicInfo(pic);
                    }
                    else
                    {
                        addsuccess += IndexPicInfoLogic.InsertIndexPicInfo(pic);
                    }
                }
                if (addsuccess == count)
                {
                    flag1 = 1;
                }
            }
            if (Request.Form["delete[]"] != null)
            {
                flag2 = 0;
                string[] delete = Request.Form["delete[]"].Split(',');
                int count = delete.Count();
                int delsuccess = 0;
                for (int i = 0; i < count; i++)
                {
                    IndexPicInfo pic = new IndexPicInfo();
                    pic.IndexPicID = Int32.Parse(delete[i]);
                    delsuccess += IndexPicInfoLogic.DeleteIndexPicInfo(pic);
                }
                if (delsuccess == count)
                {
                    flag2 = 1;
                }
            }

            if (flag1 == 1 && flag2 == 1)
            {
                ChangeStatus(true);
                BindIndexPic();
            }
            else
            {
                ChangeStatus(false);
            }
        }
        private void ChangeStatus(bool flag)
        {
            noticesuccess.Visible = flag ? true : false;
            noticefail.Visible = flag ? false : true;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "initclosenotice", "AjaxChange();divupload();", true);
        }
    }
}
